<?php
namespace app\common\model;
use think\Model;

/**
 * 红包
 */
class RedPacket extends Model {
	protected $insert = ['add_time'];
    protected function setAddTimeAttr(){
        return time();
    }

    protected static function init(){
        self::beforeInsert(function($data){
            $data['issue_time'] = strtotime($data['issue_time']);
            $data['validate_time'] = $data['issue_time'] + 12 * 3600;
        });
    }

    //地图显示红包
    public function redByMap($where = [],$lng = '',$lat = '',$having = ''){
            $list =  $this->alias('r')
                ->join(['bz_user' => 'u'],'u.id = r.user_id')
                ->join(['bz_user_position'=>'p'],'p.user_id = r.user_id')
                ->where('r.pay_status',1)->where('get_num','gt',0)->where('odd_price','gt',0)->where('issue_time','elt',time())
                ->where($where)
                ->field("r.id,p.lng,p.lat,num,price,odd_price,get_num,jump,foreign_key,u.photo_path,u.nick_name,ROUND(6378.138 * 2 * ASIN(SQRT(POW(SIN(( $lat * PI() / 180 - lat * PI() / 180) / 2),2) + COS($lat * PI() / 180) * COS(lat * PI() / 180) * POW(SIN(($lng * PI() / 180 - lng * PI() / 180) / 2),2)))) AS juli")
                ->having($having)
                ->select();

            return $list;

    }


    /**
     * [getPrice 红包领取算法]
     * @param  integer $price [剩余金额]
     * @param  integer $num   [剩余红包数量]
     * @return [type]         [本次领取金额]
     */
    public function getPrice($price = 0, $num = 0){
        if($price <= 0 || $num <= 0 || floatval($price/$num) < 0.01){
            return 0;
        }
        if(floatval($price/$num) == 0.01){
            return 0.01;
        }
        if($num == 1){
            return $price;
        }
       
        $min = 1;
        $price *= 100;
        $data = [];
        //  2                 5
        for($i = 1; $i <= $num; $i++){
            //最后一次循环直接返回剩余金额
            if($i == $num){
                $data[] = $price;
            }else{
                //本次循环最大金额  
                $s = $price;   
                $max = $price - ($num - $i) * $min;
                $now_price = mt_rand($min,$max);
                $data[] = $now_price;
                $price -= $now_price;
            }
        }
        
        return $data[mt_rand(0,$num-1)]/100;
    }

    /**
     * [redList 红包列表-关联跳转项]
     * @param  array  $where [description]
     * @return [type]        [description]
     */
    public function redList($where = [],$p = 1){
        $list = $this->alias('r')
            ->join(['bz_service' => 's'],'s.id = r.foreign_key and r.jump = 1','left')
            ->join(['bz_need' => 'n'],'n.id = r.foreign_key and r.jump = 2','left')
            ->join(['bz_user' => 'u'],'u.id = r.user_id')
            ->field('r.id as red_id,s.image as service_image,jump,s.title as service_title,n.title as need_title,n.image as need_image,u.nick_name,u.sex,u.photo_path,u.sign,r.price,u.id as user_id')
            ->where($where)->where('r.pay_status','neq',0)
            ->order('r.add_time desc')
            
            ->select();

        foreach($list as $key => $val){
            $list[$key]['service_title'] =  empty($val['service_title']) ? '' : $val['service_title'];
            $list[$key]['need_title'] =  empty($val['need_title']) ? '' : $val['need_title'];
            $list[$key]['service_image'] = empty($val['service_image']) ? "" : explode(',',$val['service_image'])[0];
            $list[$key]['need_image'] = empty($val['need_image']) ? "" : explode(',',$val['need_image'])[0];
        }
        return $list;
    }

    /**
     * [getRedList 红包领取记录-关联跳转项]
     * @param  array   $where [description]
     * @param  integer $p     [description]
     * @return [type]         [description]
     */
    public function getRedList($where = [],$p = 1){
        $list = db('red_packet_get')->alias('g')
            ->join(['bz_red_packet' => 'r'],'r.id = g.foreign_key')
            ->join(['bz_service' => 's'],'s.id = r.foreign_key and r.jump = 1','left')
            ->join(['bz_need' => 'n'],'n.id = r.foreign_key and r.jump = 2','left')
            ->join(['bz_user' => 'u'],'u.id = r.user_id')
            ->where($where)
            ->order('g.add_time desc')
            ->field('r.id as red_id,FROM_UNIXTIME(r.add_time,"%Y-%m-%d %H:%i") as add_time,s.image as service_image,jump,s.title as service_title,n.title as need_title,n.image as need_image,u.nick_name,u.sex,u.photo_path,u.sign,r.price,r.id as user_id')
        
            ->select();
        foreach($list as $key => $val){
            $list[$key]['service_title'] =  empty($val['service_title']) ? '' : $val['service_title'];
            $list[$key]['need_title'] =  empty($val['need_title']) ? '' : $val['need_title'];
            $list[$key]['service_image'] = empty($val['service_image']) ? "" : explode(',',$val['service_image'])[0];
            $list[$key]['need_image'] = empty($val['need_image']) ? "" : explode(',',$val['need_image'])[0];
        }
        return $list;
    }
}
